/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */

package l1j.server.server.clientpackets;


import l1j.opqlo.Server.L1World.L1World;
import l1j.server.echo.ClientThread;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_SendLocation;

import java.util.logging.Level;
import java.util.logging.Logger;
public class C_SendLocation extends ClientBasePacket {

	private static final String C_SEND_LOCATION = "[C] C_SendLocation";
	private static final Logger _log = Logger.getLogger(C_SendLocation.class.getName());
	@Override
	public String getType() {
		return C_SEND_LOCATION;
	}

	@Override
	public void start(final byte[] decrypt, final ClientThread client) {
		try {
			// 資料載入
			this.read(decrypt);
			final L1PcInstance pc = client.getActiveChar();
			if(pc == null){
				return;
			}
			final int type = this.readC();

			// クライアントがアクティブ,ノンアクティブ転換時に
			// オペコード 0x57 0x0dパケットを送ってくるが詳細不明の為無視
			// マップ座標転送時は0x0bパケット
			if (type == 0x0d) {
				return;
			}

			final String name = this.readS();
			if(name == null){
				return;
			}
			final int mapId = this.readH();
			final int x = this.readH();
			final int y = this.readH();
			final int msgId = this.readC();

			if (name.isEmpty()) {
				return;
			}
			final L1PcInstance target = L1World.getInstance().getPlayer(name);
			if (target != null) {
				final String sender = pc.getName();
				target.sendPackets(new S_SendLocation(type, sender, mapId, x, y, msgId));
				//将来的にtypeを使う可能性があるので送る
			}
		} catch (final Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);

		} finally {
			this.over();
		}
	}
}

